package com.ttai.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerResetKeyComponet;
import com.ttai.dagger.module.activity.ResetKeyPresenterModule;
import com.ttai.presenter.activity.GetaccountPresenter;
import com.ttai.presenter.activity.ResetKeyPresenter;
import com.ttai.presenter.activity.VerifyKeyPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.untils.Constant;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResetKey extends BaseActivity {
    @Bind(R.id.et_lastkey)
    EditText etLastkey;
    @Bind(R.id.btn_verifykey)
    Button btnVerifykey;
    @Bind(R.id.et_registercode)
    EditText etRegistercode;
    @Bind(R.id.et_newkey)
    EditText etNewkey;
    @Bind(R.id.et_keyagain)
    EditText etKeyagain;
    @Bind(R.id.btn_reset)
    Button btnReset;

    @Inject
    ResetKeyPresenter resetKeyPresenter;

    VerifyKeyPresenter verifyKeyPresenter;

    GetaccountPresenter getaccountPresenter;


    private String miYao = "";
    private String parmjson;
    private String newMiYao = "";
    private String newMiYaoAgain = "";
    private String keyTip = "";
    private String token;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetkey);

        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView barTitle = (TextView)findViewById(R.id.barTitle);
        barTitle.setText("修改密钥");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        setComponet();

        verifyKeyPresenter = new VerifyKeyPresenter(this);
        getaccountPresenter = new GetaccountPresenter(this);
        ButterKnife.bind(this);
    }
    public String getMiYao() {
        return miYao;
    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {

    }

    @Override
    public void setComponet() {
        DaggerResetKeyComponet.builder()
                .resetKeyPresenterModule(new ResetKeyPresenterModule(this))
                .build()
                .in(this);
    }

    @OnClick({R.id.btn_verifykey, R.id.btn_reset})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_verifykey:
                token = Constant.getToken();
                miYao = etLastkey.getText().toString();
                if (miYao.isEmpty()) {
                    Toast.makeText(ResetKey.this, "验证失败,密钥不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    /**
                     * SHA256密钥加密
                     */

                    String sha256Password = Constant.shaEncrypt(miYao);

                    Log.i("4", "onViewClicked: " + token + " miyao: " + miYao + " sha: " + sha256Password);
                    verifyKeyPresenter.verifyKey(token, sha256Password);

                    break;
                }

                    case R.id.btn_reset:

                        keyTip = etRegistercode.getText().toString();
                        newMiYao = etNewkey.getText().toString();
                        newMiYaoAgain = etKeyagain.getText().toString();
                        System.out.println("keyTip"+keyTip+" miyao"+newMiYao);
                        if (keyTip.isEmpty() || newMiYao.isEmpty() || newMiYaoAgain.isEmpty()){
                            Toast.makeText(ResetKey.this,"密钥提示,新密钥不能为空",Toast.LENGTH_SHORT).show();
                        }else {
                            getaccountPresenter.getAccount(token);
                        }

//                        if (newMiYao.equals(newMiYaoAgain)){
//                            System.out.println("jiu:"+Constant.shaEncrypt(miYao)+" xin:"+Constant.shaEncrypt(newMiYao) +" parm: "+parmjson+" tip"+keyTip);
//                            resetKeyPresenter.resetEncrypt(Constant.getToken(),Constant.shaEncrypt(miYao),Constant.shaEncrypt(newMiYao),parmjson,keyTip);
//                        }else {
//                            Toast.makeText(ResetKey.this,"两次密钥不一致",Toast.LENGTH_SHORT).show();
//                        }

                        break;
                }
        }

        public void getJson(String parmJson) {
        this.parmjson = parmJson;
            if (newMiYao.equals(newMiYaoAgain)){
                System.out.println("jiu:"+Constant.shaEncrypt(miYao)+" xin:"+Constant.shaEncrypt(newMiYao) +" parm: "+parmjson+" tip"+keyTip);
                try{
                    resetKeyPresenter.resetEncrypt(Constant.getToken(),Constant.shaEncrypt(miYao),Constant.shaEncrypt(newMiYao),parmjson,keyTip);

                }catch (Exception e){
                    Toast.makeText(ResetKey.this,e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(ResetKey.this,"两次密钥不一致",Toast.LENGTH_SHORT).show();
            }
    }

    public void startButton() {
        btnReset.setEnabled(true);
    }

    public String getNewMiYao() {
        newMiYao = etNewkey.getText().toString();
        return newMiYao;
    }
}
